from Business.jingjia.RepeateStockSearch import get_the_same_with_yesterday
from Utility.DingMsg import ding_sent
from Utility.TradeDate import TradeDateUtils
from getdata.StockList import StockListInterface, StockList_by_juejin
from getdata.StockTick import StockTick_by_Juejin





class Jingjia:
    def run2(self):

        res = self.get_stocklist(StockList_by_juejin())
        print(len(res))

        tomorrow_str, today_str = self.get_start_date()
        print(tomorrow_str, today_str)
        # print(res)
        rate_big3 = []

        st = StockTick_by_Juejin()
        today_df = st.get_all(res, today_str + ' 09:24:59', today_str + ' 09:25:03', frequency='tick',
                              fields='symbol,created_at,cum_volume,price')
        print('find today_df')
        # print(today_df)
        # print(today_df[today_df['symbol'] == 'SZSE.002700'])

        yesterday_df = st.get_all(res, tomorrow_str + ' 09:24:59', tomorrow_str + ' 09:25:03', frequency='tick',
                                  fields='symbol,created_at, cum_volume')
        print('find yesterday_df')
        # print(yesterday_df)
        # find SZSE.001298 in yesterday_df
        # print(yesterday_df[yesterday_df['symbol'] == 'SZSE.001298'])
        # print(yesterday_df[yesterday_df['symbol'] == 'SZSE.002629'])
        # print(yesterday_df[yesterday_df['symbol'] == 'SZSE.002700'])
        # filtered_df = yesterday_df.loc[yesterday_df['cum_volume'] >0]
        # print(filtered_df)
        # filtered_df2 = yesterday_df.loc[yesterday_df['cum_volume'] == 0]
        # print(filtered_df2)
        # return
        # for yesterday_df
        for index, row in yesterday_df.iterrows():
            try:
                # print(row)
                # get cum_volume
                yesterday_vol = row['cum_volume']
                if yesterday_vol == 0:
                    continue
                # get today_vol
                today_row=today_df[today_df['symbol'] == row['symbol']].iloc[-1]
                # print(today_row)
                today_vol =today_row['cum_volume']
                # get rate
                rate = today_vol / yesterday_vol
                # print(today_vol, yesterday_vol,rate)
                if rate > 5:
                    res = st.get(row['symbol'], tomorrow_str, tomorrow_str, frequency='1d',
                                 fields='symbol,created_at, bob,last_volume,close')
                    # print(res.iloc[-1]['close'])
                    yesterday_close=res.iloc[-1]['close']
                    # print(today_row)
                    # print(row['symbol'],today_row['price'],yesterday_close)
                    if today_row['price']>yesterday_close:
                        # print(row['symbol'])
                        # print(row['symbol'], today_row['price'], yesterday_close)
                        rate_big3.append(row['symbol'])
            except Exception as e:
                print(e)
                rate = -1
                continue
        # print(len(rate_big3))
        result = list(set(rate_big3))
        the_sames=get_the_same_with_yesterday(result)
        ding_sent(result, 'SECfcd57e9690a15dd244f0526a04212197e4988f3bdeeeb50273796df2632eec64',
                  '3b3c318e175b877b3683e1f70526376e499953ee2d4245dec626c5b657f006f5')
        ding_sent('重复：'+str(the_sames), 'SECfcd57e9690a15dd244f0526a04212197e4988f3bdeeeb50273796df2632eec64',
                  '3b3c318e175b877b3683e1f70526376e499953ee2d4245dec626c5b657f006f5')


    def get_stocklist(self, stock_list: StockListInterface):
        return stock_list.get()

    def get_start_date(self):
        du = TradeDateUtils()
        today_str = du.get_today_str()
        (tomorrow_str, today_str) = du.get_today_yesterday(today_str)
        return tomorrow_str, today_str

    def compare(self, code, today_str, tomorrow_str):
        st = StockTick_by_Juejin()
        # cum_volume
        res = st.get(code, today_str + ' 09:24:49', today_str + ' 09:26:00', frequency='tick',
                     fields='created_at,open, close, low, high,volume, bob,cum_volume')
        # print(res)
        try:

            today_vol = res.iloc[-1]['cum_volume']
            yesterday = st.get(code, tomorrow_str + ' 09:24:49', tomorrow_str + ' 09:26:00', frequency='tick',
                               fields='created_at,open, close, low, high,volume, bob,cum_volume')
            # print(res)
            tomorrow_vol = yesterday.iloc[-1]['cum_volume']
        except Exception as e:
            return 0
        if tomorrow_vol == 0:
            print(code)
            return 0
        rate = today_vol / tomorrow_vol

        # print(rate)
        return rate
